/**
 * @class 行星系数据代理
 * @author DuskyHusky 圣子
 * @description 提供行星系内相关数据和管理。
*/

import { Proxy, decorators } from 'cfw';
import { UI } from 'notifications';
import { Orbiter } from 'prefabs';
import { visit } from './visit-proxy';
const { singleton } = decorators;

@singleton
export class PlanetaryProxy extends Proxy {

    declare public static readonly instance: PlanetaryProxy;
    private constructor() { super(); }

    public get showSatellites(): boolean { return this._show_satellites; }          // 显示卫星（行星|矮星）
    public get showAxis(): boolean { return this._show_axis; }                      // 显示轴向
    public get showDiameter(): boolean { return this._show_diameter; }              // 显示直径
    public get showMagnetics(): boolean { return this._show_magnetics; }            // 显示磁场（行星）
    public get showEquatorialPlane(): boolean { return this._show_equatorial; }     // 显示赤道面
    public get showEclipticPlane(): boolean { return this._show_ecliptic; }         // 显示黄道面
    public get showLunarPlane(): boolean { return this._show_lunar && this.centerName === 'Earth'; } // 显示白道面
    public get showLunarPlaneUI(): boolean { return this._show_lunar; }             // 显示白道面UI
    public get showTideLock(): boolean { return this._show_tide_lock; }               // 显示潮汐锁定
    public get showTideLockUI(): boolean { return this._show_tide_lock && this._center.cat === 'satellite'; }  // 显示潮汐锁定UI
    public get showConstitute(): boolean { return this._show_constitute; }          // 显示内部构成（行星）
    public get showArtificial(): boolean { return this._show_artificial; }          // 显示人造物体

    public get satellites(): satellite[] { return this._satellites; }               // 当前系统内的卫星
    public get systemScope(): pow10 { return this._systemScope; }                   // 当前行星系统范围
    public get center(): Orbiter | null { return this._center; }                    // 当前中心行星（矮星/小行星/带/彗星）
    public get centerName(): celestial {                                            // 当前中心行星（矮星/小行星/带/彗星）名称
        if (this.unbelt) return this._center?.config?.name;
        else return visit.transInfo.name;
    }
    public get unbelt(): boolean { return this._center && this.center.cat !== 'belts'; }

    public getStateByName(name: string): boolean {
        switch (name) {
            case '卫星': return this.showSatellites;
            case '地轴': return this.showAxis;
            case '直径': return this.showDiameter;
            case '磁场': return this.showMagnetics;
            case '赤道面': return this.showEquatorialPlane;
            case '黄道面': return this.showEclipticPlane;
            case '白道面': return this.showLunarPlaneUI;
            case '潮汐锁定': return this.showTideLockUI;
            case '人造物': return this.showArtificial;
            default: return null;
        }
    }

    public static AsteroidRectangular = {
        BASE_SIZE: 2e-8,
        SIZE_RANGE: [0.8, 3],
        SCALE_RANGE: [1, 2],
        POS_RANGE: [1e-5, 1e-6, 1e-5],
    }

    private _show_satellites: boolean = true;
    private _show_axis: boolean = false;
    private _show_diameter: boolean = false;
    private _show_magnetics: boolean = false;
    private _show_equatorial: boolean = false;
    private _show_ecliptic: boolean = false;
    private _show_lunar: boolean = false;
    private _show_tide_lock: boolean = false;
    private _show_constitute: boolean = false;
    private _show_artificial: boolean = false;

    private _satellites: satellite[] = [];
    private _systemScope: pow10 = 0;
    private _center: Orbiter = null;

    public reloadOrbiterParams(center: Orbiter, satellites?: satellite[], systemScope?: pow10): void {
        this._center = center;
        this._satellites.length = this._systemScope = 0;
        if (notEmptyArr(satellites)) {
            this._satellites.push(...satellites);
        }
        this._systemScope = systemScope;
    }

    protected init(): void {
        this.on(SATELLITES, (arg: boolean) => this._show_satellites = arg);
        this.on(AXIS, (arg: boolean) => this._show_axis = arg);
        this.on(DIAMETER, (arg: boolean) => this._show_diameter = arg);
        this.on(MAGNETICS, (arg: boolean) => this._show_magnetics = arg);
        this.on(ECLIPTIC, (arg: boolean) => this._show_ecliptic = arg);
        this.on(EQUATORIAL, (arg: boolean) => this._show_equatorial = arg);
        this.on(LUNAR, (arg: boolean) => this._show_lunar = arg);
        this.on(TIDE_LOCK, (arg: boolean) => this._show_tide_lock = arg);
        this.on(CONSTITUTE, (arg: boolean) => this._show_constitute = arg);
        this.on(ARTIFICIAL, (arg: boolean) => this._show_artificial = arg);
    }
}

const { Local: { SATELLITES, DIAMETER, AXIS, MAGNETICS, LUNAR, EQUATORIAL, ECLIPTIC, TIDE_LOCK, CONSTITUTE, ARTIFICIAL } } = UI;

export const planetary = PlanetaryProxy.instance;